home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr37 / aussiebb.zip / CAPI.ZIP / JAMMBINI.C < prev    next >
C/C++ Source or Header  |  1993-07-01  |  15KB  |  449 lines

  1. /*
  2. **  JAM(mbp) - The Joaquim-Andrew-Mats Message Base Proposal
  3. **
  4. **  C API
  5. **
  6. **  Written by Joaquim Homrighausen.
  7. **
  8. **  ----------------------------------------------------------------------
  9. **
  10. **  jammbini.c (JAMmb)
  11. **
  12. **  Initialization, open, and close functions for message base
  13. **
  14. **  Copyright 1993 Joaquim Homrighausen, Andrew Milner, Mats Birch, and
  15. **  Mats Wallin. ALL RIGHTS RESERVED.
  16. **
  17. **  93-06-28    JoHo
  18. **  Initial coding
  19. */
  20. #define JAMCAPI 1
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. #ifdef __sparc__
  26. #include <memory.h>
  27. #endif
  28.  
  29. #include "jammb.h"
  30.  
  31. /*
  32. **  Open message base pointed to by API structure. Returns 1 on success,
  33. **  0 on failure.
  34. */
  35. int _JAMPROC JAMmbOpen(JAMAPIRECptr apirec)
  36. {
  37.     CHAR8   FileName[200];
  38.  
  39.     /* Make sure it's not already open */
  40.     if (apirec->isOpen)
  41.         {
  42.         apirec->APImsg=JAMAPIMSG_ISOPEN;
  43.         return (1);
  44.         }
  45.     else 
  46.         apirec->APImsg=JAMAPIMSG_NOTHING;
  47.  
  48.     /* .JHR file */
  49.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_HDRFILE);
  50.     apirec->HdrHandle=apirec->OpenFunc(apirec, FileName);
  51.     if (apirec->HdrHandle<0)
  52.         return (0);
  53.  
  54.     if (apirec->ReadFunc(apirec, apirec->HdrHandle, &apirec->HdrInfo, (INT32)sizeof(JAMHDRINFO))!=(INT32)sizeof(JAMHDRINFO))
  55.         {
  56.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  57.         return (0);
  58.         }
  59.     if (apirec->HdrInfo.BaseMsgNum==0L)
  60.         apirec->HdrInfo.BaseMsgNum=1L;
  61.  
  62.     /* .JDT file */
  63.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_TXTFILE);
  64.     apirec->TxtHandle=apirec->OpenFunc(apirec, FileName);
  65.     if (apirec->TxtHandle<0)
  66.         {
  67.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  68.         return (0);
  69.         }
  70.  
  71.     /* .JDX file */
  72.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_IDXFILE);
  73.     apirec->IdxHandle=apirec->OpenFunc(apirec, FileName);
  74.     if (apirec->IdxHandle<0)
  75.         {
  76.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  77.         apirec->CloseFunc(apirec, apirec->TxtHandle);
  78.         return (0);
  79.         }
  80.  
  81.     /* .JLR file */
  82.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_LRDFILE);
  83.     apirec->LrdHandle=apirec->OpenFunc(apirec, FileName);
  84.     if (apirec->LrdHandle<0)
  85.         {
  86.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  87.         apirec->CloseFunc(apirec, apirec->TxtHandle);
  88.         apirec->CloseFunc(apirec, apirec->IdxHandle);
  89.         return (0);
  90.         }
  91.  
  92.     apirec->isOpen=1;
  93.     return (1);
  94. }
  95.  
  96.  
  97. /*
  98. **  Close message base pointed to by API structure. Returns 1 on success,
  99. **  0 on failure.
  100. */
  101. int _JAMPROC JAMmbClose(JAMAPIRECptr apirec)
  102. {
  103.     /* Close files */
  104.     if (apirec->isOpen)
  105.         {
  106.         apirec->APImsg=JAMAPIMSG_NOTHING;
  107.  
  108.         /* Make sure header file is unlocked */
  109.         if (apirec->HaveLock)
  110.             apirec->LockFunc(apirec, 0);
  111.  
  112.         /* Close all handles */
  113.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  114.         apirec->CloseFunc(apirec, apirec->TxtHandle);
  115.         apirec->CloseFunc(apirec, apirec->IdxHandle);
  116.         apirec->CloseFunc(apirec, apirec->LrdHandle);
  117.  
  118.         apirec->HdrHandle=
  119.             apirec->TxtHandle=
  120.                 apirec->IdxHandle=
  121.                     apirec->LrdHandle=-1;
  122.  
  123.         /* Set flag */
  124.         apirec->isOpen=0;
  125.         }
  126.     else
  127.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  128.  
  129.     return (1);
  130. }
  131.  
  132.  
  133. /*
  134. **  Remove message base pointed to by API structure. Returns 1 on success,
  135. **  0 on failure.
  136. */
  137. int _JAMPROC JAMmbUnlink(JAMAPIRECptr apirec)
  138. {
  139.     CHAR8 FileName[200];
  140.     int J1, J2, J3, J4;
  141.  
  142.     apirec->APImsg=JAMAPIMSG_NOTHING;
  143.     if (apirec->isOpen)
  144.         {
  145.         apirec->APImsg=JAMAPIMSG_ISOPEN;
  146.         return (0);
  147.         }
  148.  
  149.     /* .JHR file */
  150.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_HDRFILE);
  151.     J1=apirec->UnlinkFunc(apirec, FileName);
  152.  
  153.     /* .JDT file */
  154.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_TXTFILE);
  155.     J2=apirec->UnlinkFunc(apirec, FileName);
  156.  
  157.     /* .JDX file */
  158.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_IDXFILE);
  159.     J3=apirec->UnlinkFunc(apirec, FileName);
  160.  
  161.     /* .JLR file */
  162.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_LRDFILE);
  163.     J4=apirec->UnlinkFunc(apirec, FileName);
  164.  
  165.     return (!J1 && !J2 && !J3 && !J4);
  166. }
  167.  
  168.  
  169. /*
  170. **  Create new message base pointed to by API structure. Returns 1 on
  171. **  success, 0 on failure.
  172. */
  173. int _JAMPROC JAMmbCreate(JAMAPIRECptr apirec)
  174. {
  175.     CHAR8 FileName[200];
  176.  
  177.     /* Make sure it's not already open */
  178.     if (apirec->isOpen)
  179.         {
  180.         apirec->APImsg=JAMAPIMSG_ISOPEN;
  181.         return (1);
  182.         }
  183.     else 
  184.         apirec->APImsg=JAMAPIMSG_NOTHING;
  185.  
  186.     /* .JHR file */
  187.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_HDRFILE);
  188.     apirec->HdrHandle=apirec->CreateFunc(apirec, FileName);
  189.     if (apirec->HdrHandle<0)
  190.         return (0);
  191.  
  192.     memset(&apirec->HdrInfo, 0, sizeof(JAMHDRINFO));
  193.     strcpy(apirec->HdrInfo.Signature, HEADERSIGNATURE);
  194.     apirec->HdrInfo.DateCreated=JAMsysTime(NULL);
  195.     apirec->HdrInfo.PasswordCRC=0xffffffffL;
  196.     apirec->HdrInfo.BaseMsgNum=1L;
  197.  
  198.     if (apirec->WriteFunc(apirec, apirec->HdrHandle, &apirec->HdrInfo, (INT32)sizeof(JAMHDRINFO))!=(INT32)sizeof(JAMHDRINFO))
  199.         {
  200.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  201.         return (0);
  202.         }
  203.  
  204.     /* .JDT file */
  205.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_TXTFILE);
  206.     apirec->TxtHandle=apirec->CreateFunc(apirec, FileName);
  207.     if (apirec->TxtHandle<0)
  208.         {
  209.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  210.         return (0);
  211.         }
  212.  
  213.     /* .JDX file */
  214.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_IDXFILE);
  215.     apirec->IdxHandle=apirec->CreateFunc(apirec, FileName);
  216.     if (apirec->IdxHandle<0)
  217.         {
  218.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  219.         apirec->CloseFunc(apirec, apirec->TxtHandle);
  220.         return (0);
  221.         }
  222.  
  223.     /* .JLR file */
  224.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_LRDFILE);
  225.     apirec->LrdHandle=apirec->CreateFunc(apirec, FileName);
  226.     if (apirec->LrdHandle<0)
  227.         {
  228.         apirec->CloseFunc(apirec, apirec->HdrHandle);
  229.         apirec->CloseFunc(apirec, apirec->TxtHandle);
  230.         apirec->CloseFunc(apirec, apirec->IdxHandle);
  231.         return (0);
  232.         }
  233.  
  234.     apirec->isOpen=1;
  235.     return (1);
  236. }
  237.  
  238.  
  239. /*
  240. **  Reindex messages in an open message base pointed to by API structure.
  241. **  The message base must be locked by the application prior to calling this
  242. **  function. Returns 1 on success, 0 on failure.
  243. */
  244. int _JAMPROC JAMmbReIndex(JAMAPIRECptr apirec)
  245. {
  246.     CHAR8 FileName[200], JunkBuf[110];
  247.     JAMIDXREC IdxRec;
  248.     JAMSUBFIELD *SubPtr;
  249.     INT32 Junk, NextOffset, CurIdxOffset, NextIdxOffset, IdxSize;
  250.     UINT32 JunkLong, SubDatLen;
  251.     INT16 Done, Error, FoundField;
  252.  
  253.     /* Make sure it's open */
  254.     if (!apirec->isOpen)
  255.         {
  256.         apirec->APImsg=JAMAPIMSG_ISNOTOPEN;
  257.         return (0);
  258.         }
  259.  
  260.     /* Make sure we have lock */
  261.     if (!apirec->HaveLock)
  262.         {
  263.         apirec->APImsg=JAMAPIMSG_ISNOTLOCKED;
  264.         return (0);
  265.         }
  266.  
  267.     /* Close index file */
  268.     if (apirec->IdxHandle>=0)
  269.         apirec->CloseFunc(apirec, apirec->IdxHandle);
  270.  
  271.     /* Create new index file */
  272.     sprintf(FileName, "%s%s", apirec->BaseName, EXT_IDXFILE);
  273.     apirec->IdxHandle=apirec->CreateFunc(apirec, FileName);
  274.     if (apirec->IdxHandle<0)
  275.         {
  276.         apirec->APImsg=JAMAPIMSG_CANTMKFILE;
  277.         return (0);
  278.         }
  279.  
  280.     /* Process header file */
  281.     NextOffset=(INT32)sizeof(JAMHDRINFO);
  282.     CurIdxOffset=0L;
  283.     IdxSize=0L;
  284.     Done=Error=0L;
  285.     SubPtr=(JAMSUBFIELD *)JunkBuf;
  286.  
  287.     while (!Done && !Error)
  288.         {
  289.         /* Seek to correct position in header file */
  290.         if (apirec->SeekFunc(apirec, apirec->HdrHandle, JAMSEEK_SET, NextOffset)!=NextOffset)
  291.             {
  292.             apirec->APImsg=JAMAPIMSG_SEEKERROR;
  293.             Error=1;
  294.             }
  295.         else
  296.             {
  297.             /* Read header */
  298.             Junk=apirec->ReadFunc(apirec, apirec->HdrHandle, &apirec->Hdr, (INT32)sizeof(JAMHDR));
  299.             if (Junk!=(INT32)sizeof(JAMHDR))
  300.                 {
  301.                 if (!Junk)
  302.                     /* Nothing left to read */
  303.                     Done=1;
  304.                 else
  305.                     {
  306.                     /* Read error */
  307.                     apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  308.                     Error=1;
  309.                     }
  310.                 }
  311.             else
  312.                 {
  313.                 /* Set values */
  314.                 IdxRec.HdrOffset=(UINT32)NextOffset;
  315.  
  316.                 /* Search subfields if we have any and this hasn't been deleted */
  317.                 if (apirec->Hdr.SubfieldLen && !(apirec->Hdr.Attribute & MSG_DELETED))
  318.                     {
  319.                     FoundField=0;
  320.                     JunkLong=0L;
  321.                     do
  322.                         {
  323.                         Junk=apirec->ReadFunc(apirec, apirec->HdrHandle, &JunkBuf, sizeof(JAMBINSUBFIELD));
  324.                         if (Junk!=sizeof(JAMBINSUBFIELD))
  325.                             {
  326.                             if (!Junk)
  327.                                 Done=1;
  328.                             else
  329.                                 {
  330.                                 apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  331.                                 Error=1;
  332.                                 }
  333.                             }
  334.                         else
  335.                             {
  336.                             JunkLong+=sizeof(JAMBINSUBFIELD);
  337.                             SubDatLen=SubPtr->DatLen;
  338.  
  339.                             /* Is this what we're looking for? */
  340.                             if (SubPtr->LoID==JAMSFLD_RECVRNAME)
  341.                                 {
  342.                                 /* Yes, read username */
  343.                                 Junk=apirec->ReadFunc(apirec, apirec->HdrHandle, &JunkBuf, (INT32)SubDatLen);
  344.                                 if (Junk==(INT32)SubDatLen)
  345.                                     FoundField=1;
  346.                                 else
  347.                                     {
  348.                                     apirec->APImsg=JAMAPIMSG_CANTRDFILE;
  349.                                     Error=1;
  350.                                     }
  351.                                 }
  352.                             else
  353.                                 /* No, skip to next header */
  354.                                 {
  355.                                 SubDatLen=JAMsysAlign(SubDatLen);
  356.                                 JunkLong+=SubDatLen;
  357.                                 if (apirec->SeekFunc(apirec, apirec->HdrHandle, JAMSEEK_CUR, SubDatLen)<0L)
  358.                                     {
  359.                                     apirec->APImsg=JAMAPIMSG_SEEKERROR;
  360.                                     Error=1;
  361.                                     }
  362.                                 }/*Skip to next*/
  363.                             }/*ReadOK*/
  364.                         }
  365.                     while (!Error && !FoundField && !Done && (INT32)JunkLong<(INT32)apirec->Hdr.SubfieldLen);
  366.  
  367.                     if (FoundField)
  368.                         {
  369.                         *((UCHAR8 _JAMFAR *)(JAMsysAddFarPtr (JunkBuf, SubDatLen))) = '\0';
  370.                         strlwr (JunkBuf);
  371.                         IdxRec.UserCRC=JAMsysCrc32((void _JAMFAR *)&JunkBuf, (UINT16)SubDatLen, 0xffffffffL);
  372.                         }
  373.                     else
  374.                         IdxRec.UserCRC=0xffffffffL;
  375.                     }
  376.                 else
  377.                     /* No subfields */
  378.                     IdxRec.UserCRC=0xffffffffL;
  379.  
  380.                 /* Skip message if invalid message number */
  381.                 if (apirec->Hdr.MsgNum>=apirec->HdrInfo.BaseMsgNum)
  382.                     {
  383.                     if (!Error)
  384.                         {
  385.                         NextIdxOffset=(apirec->Hdr.MsgNum-apirec->HdrInfo.BaseMsgNum)*sizeof(JAMIDXREC);
  386.                         if (CurIdxOffset!=NextIdxOffset)
  387.                             {
  388.                             if (NextIdxOffset>IdxSize)
  389.                                 {
  390.                                 if (apirec->SeekFunc(apirec, apirec->IdxHandle, JAMSEEK_SET, IdxSize)!=IdxSize)
  391.                                     {
  392.                                     apirec->APImsg=JAMAPIMSG_SEEKERROR;
  393.                                     Error=1;
  394.                                     }
  395.                                 while (!Error && IdxSize!=NextIdxOffset)
  396.                                     {
  397.                                     JAMIDXREC   TmpIdxRec = {0xffffffffL,0xffffffffL};
  398.  
  399.                                     if (apirec->WriteFunc(apirec, apirec->IdxHandle, &TmpIdxRec, (INT32)sizeof(JAMIDXREC))!=(INT32)sizeof(JAMIDXREC))
  400.                                         {
  401.                                         apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  402.                                         Error=1;
  403.                                         }
  404.                                     else
  405.                                         IdxSize+=sizeof(JAMIDXREC);
  406.                                     }
  407.                                 }
  408.                             else
  409.                                 {
  410.                                 if (apirec->SeekFunc(apirec, apirec->IdxHandle, JAMSEEK_SET, NextIdxOffset)!=NextIdxOffset)
  411.                                     {
  412.                                     apirec->APImsg=JAMAPIMSG_SEEKERROR;
  413.                                     Error=1;
  414.                                     }
  415.                                 }
  416.                             }
  417.                         }
  418.  
  419.                     if (!Error)
  420.                         {
  421.                         /* Write index record */
  422.                         if (apirec->WriteFunc(apirec, apirec->IdxHandle, &IdxRec, (INT32)sizeof(JAMIDXREC))!=(INT32)sizeof(JAMIDXREC))
  423.                             {
  424.                             /* Write error */
  425.                             apirec->APImsg=JAMAPIMSG_CANTWRFILE;
  426.                             Error=1;
  427.                             }
  428.                         else
  429.                             {
  430.                             CurIdxOffset=NextIdxOffset+sizeof(JAMIDXREC);
  431.                             if (CurIdxOffset>IdxSize)
  432.                                 IdxSize=CurIdxOffset;
  433.  
  434.                             /* Calculate next header offset */
  435.                             NextOffset+=(INT32)sizeof(JAMHDR);
  436.                             if (apirec->Hdr.SubfieldLen)
  437.                                 NextOffset+=(INT32)apirec->Hdr.SubfieldLen;
  438.                             }
  439.                         }
  440.                     }
  441.                 }
  442.             }
  443.         }/*while*/
  444.  
  445.     return (!Error);
  446. }
  447.  
  448. /* end of file "jammbini.c" */
  449.